﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PSTUTemplate.Models.DBObject;
using PSTUTemplate.Models;
using PSTUTemplate.Models.Object.ViewModels;
using System.Web.Helpers;
using System.IO;

namespace PSTUTemplate.Controllers
{
    public class ProfileController : Controller
    {
        Repository rep = new Repository();

        //
        // GET: /Profile/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Profile/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Profile/Create

        public ActionResult Create()
        {
            Profile myProfile = new Profile();
            return View(myProfile);
        }

        //
        // POST: /Profile/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Profile/Edit/5

        public ActionResult Edit()
        {
            string loggeduser = User.Identity.Name;
            Profile myProfile = rep.GetProfileByLoggedUserName(loggeduser);

            if (myProfile == null)
            {
                myProfile = new Profile();
            }
            return View(myProfile);
        }

        //
        // POST: /Profile/Edit/5

        [HttpPost]
        public ActionResult Edit(Profile profile, HttpPostedFileBase file)
        {
            bool isNewProfile = false;
            string loggeduser = User.Identity.Name;
            Profile myProfile = rep.GetProfileByLoggedUserName(loggeduser);
            if (myProfile == null)
            {
                myProfile = new Profile();
                isNewProfile = true;
            }
            myProfile.VillageRoadArea = profile.VillageRoadArea;
            myProfile.District = profile.District;
            myProfile.Email = profile.Email;
            myProfile.Faculty = profile.Faculty;
            myProfile.FathersName = profile.FathersName;
            myProfile.IsTribial = profile.IsTribial;
            myProfile.MothersName = profile.MothersName;
            myProfile.Name = profile.Name;
            myProfile.Nationality = profile.Nationality;
            myProfile.Phone = profile.Phone;
            myProfile.PostOffice = profile.PostOffice;
            myProfile.Religion = profile.Religion;

            if (profile.Roll.Length != 7)
            {
                ModelState.AddModelError("Roll", "Roll must be 7 digit long!");
            }
            else
            {
                myProfile.Roll = profile.Roll;
            }
            myProfile.RegistrationNo = profile.RegistrationNo;
            myProfile.StartSession = profile.StartSession;
            myProfile.Thana = profile.Thana;

            myProfile.Level = profile.Level;
            myProfile.Semester = profile.Semester;
            myProfile.HallName = profile.HallName;
            myProfile.HallBorderNo = profile.HallBorderNo;
            myProfile.RoomNo = profile.RoomNo;



            string fileName = "";

            if (file.ContentLength > 0)
            {
                fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Uploads"), fileName);
                file.SaveAs(path);
            }

            myProfile.Avatar = fileName;

            if (ModelState.IsValid)
            {
                if (isNewProfile)
                {
                    myProfile.LoggedUser = loggeduser;
                    rep.AddProfile(myProfile);
                }
                else
                {
                    rep.Save();
                }
                return RedirectToAction("Index", "Home");
            }

            return View(profile);
        }

        //
        // GET: /Profile/Delete/5

        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Profile/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        [HttpGet]
        public ActionResult Upload()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Upload(HttpPostedFileBase file)
        {

            if (file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                var path = Path.Combine(Server.MapPath("~/Uploads"), fileName);
                file.SaveAs(path);
            }

            return View();
        }
    }
}
